package com.cookie.controller;

import com.cookie.api.exception.JsonResponseException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

/**
 * @author DELL
 */
@RestController
@RequestMapping("/cookie")
@Api("操作Cookie")
public class CookieController {

    @ApiOperation(value = "Http增加cookie")
    @PostMapping("/add")
    public Object setCookie(@ApiParam(value = "名字", required = true) @RequestParam("name") String name,
                            @ApiParam(value = "值", required = true) @RequestParam("value") String value,
                            @ApiParam(value = "绝对过期时间 dd-MM-yyyy HH:mm:ss") @RequestParam(value = "expire", required = false) String expire,
                            @ApiParam(value = "相对过期时间") @RequestParam(value = "maxAge", defaultValue = "-1") Long maxAge,
                            @ApiParam(value = "路径") @RequestParam(value = "path", required = false) String path,
                            @ApiParam(value = "域") @RequestParam(value = "domain", required = false) String domain,
                            @ApiParam(value = "安全性") @RequestParam(value = "secure", defaultValue = "false") Boolean secure,
                            @ApiParam(value = "httpOnly") @RequestParam(value = "httpOnly", defaultValue = "false") Boolean httpOnly,
                            HttpServletResponse response) {
        try {
            StringBuilder builder = new StringBuilder();
            builder.append(name).append("=").append(value).append(";");
            if (null != expire) {
                builder.append("Expires=").append(expire).append(";");
            }
            if (null != maxAge) {
                builder.append("Max-Age=").append(maxAge).append(";");
            }
            if (null != path) {
                builder.append("path").append(path).append(";");
            }
            if (null != domain) {
                builder.append("Domain=").append(domain).append(";");
            }
            if (secure) {
                builder.append("secure").append(";");
            }
            if (httpOnly) {
                builder.append("HttpOnly").append(";");
            }
            response.setHeader("Set-Cookie", builder.toString());
            return builder;
        } catch (Exception e) {
            return e;
        }
    }

    @ApiOperation(value = "Servlet增加cookie")
    @PostMapping("/servlet/add")
    public Object setCookieServletApi(@ApiParam(value = "名字", required = true) @RequestParam("name") String name,
                                      @ApiParam(value = "值", required = true) @RequestParam("value") String value,
                                      @ApiParam(value = "相对过期时间") @RequestParam(value = "maxAge", defaultValue = "-1") Long maxAge,
                                      @ApiParam(value = "路径") @RequestParam(value = "path", required = false) String path,
                                      @ApiParam(value = "域") @RequestParam(value = "domain", required = false) String domain,
                                      @ApiParam(value = "安全性") @RequestParam(value = "secure", defaultValue = "false") Boolean secure,
                                      @ApiParam(value = "httpOnly") @RequestParam(value = "httpOnly", defaultValue = "false") Boolean httpOnly,
                                      HttpServletResponse response) {
        try {
            Cookie cookie = new Cookie(name, value);
            if (null != maxAge) {
                cookie.setMaxAge(Math.toIntExact(maxAge));
            }
            if (null != path) {
                cookie.setPath(path);
            }
            if (null != domain) {
                cookie.setDomain(domain);
            }
            if (secure) {
                cookie.setSecure(true);
            }
            if (httpOnly) {
                cookie.setHttpOnly(true);
            }
            response.addCookie(cookie);
            return cookie;
        } catch (Exception e) {
            return e;
        }
    }

    @GetMapping("list")
    @ApiOperation(value = "获取所有cookie")
    public Object list(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        return Optional.ofNullable(cookies)
            .orElseThrow(() -> new JsonResponseException("cookie.list.empty"));
    }


}
